<template>
  <el-form
    :model="ruleForm"
    status-icon
    :rules="rules"
    ref="ruleForm"
    label-width="100px"
    class="demo-ruleForm"
  >
    <el-form-item label="账号" prop="passWord">
      <el-input disabled type="text" :value="data.username"></el-input>
    </el-form-item>
    <el-form-item label="密码" prop="passWord">
      <el-input disabled type="password" v-model.number="ruleForm.passWord"></el-input>
    </el-form-item>
    <el-form-item label="邮箱" prop="email">
      <el-input
        type="text"
        v-model="ruleForm.email"
      ></el-input>
    </el-form-item>
    <el-form-item label="手机号" prop="phone">
      <el-input
        type="text"
        v-model="ruleForm.phone"
      ></el-input>
    </el-form-item>

    <el-form-item>
      <el-button type="primary" @click="submitForm('ruleForm')"
      >修改
      </el-button>
      <el-button @click="$root.$emit('modifyData',false)">取消</el-button>
    </el-form-item>
  </el-form>
</template>

<script lang="ts">
import { Component, Prop, Vue } from 'vue-property-decorator'
import { modifyUser } from '@/api/user'

@Component
export default class ModifyUsersodify extends Vue {
  public ruleForm = {
    email: this.data.email,
    phone: this.data.mobile
  }

  public rules = {
    email: [
      {
        validator: this.validationEmail,
        trigger: 'blur'
      }
    ],
    phone: [
      {
        validator: this.validationPhone,
        trigger: 'blur'
      }
    ]
  }

  @Prop()
  data?:any

  created () {
    console.log(this.data)
  }

  // 邮箱验证
  validationEmail (rule: any, value: string, callback: any) {
    if (value === '') return callback(new Error('请输入邮箱'))
    if (!/^\w{2,12}@\w{1,5}\.com$/.test(value)) {
      return callback(new Error('邮箱格式错'))
    }

    callback()
  }

  // 手机验证
  validationPhone (rule: any, value: string, callback: any) {
    if (value === '') return callback(new Error('请输入手机号'))
    if (!/^1[35789]\d{9}$/.test(value)) {
      return callback(new Error('手机号格式错误'))
    }

    callback()
  }

  // 点击提交按钮
  submitForm (formName: string) {
    (this.$refs[formName] as any).validate(async (valid: unknown) => {
      if (!valid) return false

      const { meta, data } = await modifyUser(this.data.id, {
        email: this.ruleForm.email,
        mobile: +this.ruleForm.phone
      })
      console.log(meta, data)
      if (!(meta.status === 200)) {
        return this.$message({
          message: meta.msg,
          type: 'error'
        })
      }
      this.$message({
        message: meta.msg,
        type: 'success'
      })
      this.$root.$emit('modifyData', true)
    })
  }

  // 成功提示框
  open2 (smg = '登陆成功') {
    this.$message({
      message: smg,
      type: 'success'
    })
  }

  // 清除输入星系
  resetForm (formName: string) {
    (this.$refs[formName] as any).resetFields()
  }
}
</script>

<style lang="less" scoped>
.demo-ruleForm{
  background-color: #ece9e9;
  height: 300px;
  width: 600px;
  position: fixed;
  z-index: 99;
  top: 50%;
  left:50%;
  transform: translate(-50%,-50%);
}
</style>
